<?php
# header of each page
function head($title='', $meta_description='', $meta_keywords='', $active='') {
header("Content-type: text/html; charset=UTF-8");
?>
<!DOCTYPE html>
<html>
<head>
<title><?=$title?></title>
<meta name="description" content="<?=$meta_description?>" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">

<link href='http://fonts.googleapis.com/css?family=Open+Sans:300,400&subset=latin,cyrillic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="<?=HTML_BASE?>css/reset.css">
<link rel="stylesheet" type="text/css" href="<?=HTML_BASE?>css/style.css?v=1">
<script src="<?=HTML_BASE?>js/modernizr.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
<script src="<?=HTML_BASE?>js/script.js" type="text/javascript"></script>

<meta name="google-site-verification" content="Pk_sxOL2-tTT-JPD-VR7CNulCcVLiqBaBHzYdlSsbpc" />
</head>
<body>
<div id="wrap">
	
    
  
<?php	
}
function menue()
{
	?>
    
	<?php
}
# footer of each page
function foot() {
?>
		
	
</div>

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-6682949-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type =
'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
  })();

</script>

</body>
</html>
 
        
<?php
}

# a redirecting function that adds the session ID if cookies are disabled and stops the execution of the script
function redirect($location) {
	if (!$location) error("Missing arguments.");

	$a = parse_url($location);
	if (!isset($a['query'])) {
		$a['query'] = '';
	}
	
	$session_delimiter = ($a['query'])?'&':'?';
	header("Location: $location".addsession($session_delimiter));
	exit;
}

# adding the session_id when cookies are disabled
function addsession($symbol="?") {
	return (!isset($_COOKIE[session_name()])&&session_id())?$symbol.session_name()."=".session_id():"";
}

function escape($string,$encoding=false) {
  return mysql_real_escape_string( (($encoding)?encoding($string):$string) );
}

function encoding($string) {
  return iconv('ISO-8859-1', 'UTF-8', $string);
}

function sth($string) {
  return htmlspecialchars(stripslashes($string));
}

# checks whether an email is valid or not
function checkEmail($email) {
	return preg_match("/^[A-z0-9\.\-]+@(?:[A-z0-9\-]+\.)+[A-z0-9]{1,6}$/", $email);
}

# Cut a text to specified chracters length
function text_format($text, $length) {
	$str = '';
	$text_length = mb_strlen($text, "UTF-8");
	 
	if ($text_length < $length) {
		$str = $text;		
	} else {
		$position_end = strpos($text, ' ', $length);
		$str = substr($text, 0, ($position_end)?$position_end:$text_length);
	}
	
	return $str;
}

function text_format_blog($text, $length) {
	$str = '';
	$text_length = mb_strlen($text, "UTF-8");
	 
	if ($text_length < $length) {
		$str = $text;		
	} else {
		$position_end = strpos($text, '<hr />', $length);
		$str = substr($text, 0, ($position_end)?$position_end:$text_length);
	}
	
	return $str;
}


# general error function
function error($s="An error occured.") {
  if (!headers_sent()) {
    head("Error");
    echo "<table cellpadding=4 width=".SITE_WIDTH." align=center border=0><tr><td align=center class=title>$s</td></tr></table>";
    foot();
  }
  else
    echo $s;
  exit;
}

# debug function for dumping a variable value
function pp($var, $stop=false) {
  echo "<pre>";
  print_r($var);
  echo "</pre>";
  if ($stop) exit;
}

# Generate numbers
function generateNumber () {

  # start with a blank number
  $number = "";

  # define possible characters
  $possible = "0123456789"; 

  # pick a random character from the possible ones
  $number = substr($possible, mt_rand(0, strlen($possible)-1), 1);

  # done!
  return $number;

}



function getImage($path) {

  if(is_dir($path))
	{
	$dir = opendir( $path );
	$failnum = 0;
	while ( false != ( $filename = readdir($dir)))
		   { 
		   if (( $filename != "." ) and ( $filename != ".." ))
			  { 
				  if($failnum > 0)
				  {
					break;  
				  }else{
				  $f = HTML_BASE.$path.$filename;
				  }
				$failnum ++; 
			  }
		   }
	closedir($dir);
	return $f;
	}
  

}

function image_likes($album,$image)
{
	$sql = "SELECT ip FROM image_likes WHERE album_id = '".$album."' AND iamge = '".$image."'";
	$likes = query($sql);
	return mysql_numrows($likes);
}


function ip_likes($album,$image)
{
	$ip = $_SERVER["REMOTE_ADDR"];
	
	$sql = "SELECT ip FROM image_likes WHERE album_id = '".$album."' AND iamge = '".$image."' AND ip = '".$ip."'";
	$likes = query($sql);
	
	if(mysql_numrows($likes))
	{
		return 'class="like"';
	}
}


function image_title($album,$image)
{
	$sql_imds = "SELECT description FROM
						album_images_description
					WHERE
						album_id = '".$album."'
						AND image = '".$image."'
						AND lang_id = '".$_SESSION['lang_id']."'
					";
		$result_imds = query($sql_imds);
		$imds = mysql_fetch_object($result_imds);
		
		return  $imds->description;
}

?>